package com.yonyou.train.service;

import com.alibaba.fastjson.JSONObject;
import com.yonyou.train.dao.AuthDao;
import com.yonyou.train.dao.UserDao;
import com.yonyou.train.model.Auth;
import com.yonyou.train.model.User;
import com.yonyou.train.yonyouec.SyncEcUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class AuthService {
    @Autowired
    private AuthDao authMapper;
    private static final Logger LOG = LoggerFactory.getLogger(SyncEcUser.class);
    public List<Map>queryAuth(){
        return authMapper.queryAuth();
    }

    public Auth queryUserAuth(String usrid){
        return authMapper.queryUserAuth(usrid);
    }

    public void updateAuth(Map map){
        List<Map> list=(List<Map>)map.get("info");
        for(Map info:list){
            long id=transLong(info.get("id"));
            String workstatus= info.get("workstatus").toString();
            String filed=info.get("filed").toString();
            int ispublish=(int)info.get("ispublish");
            int issignup=(int)info.get("issignup");
            int isreport=(int)info.get("isreport");
            authMapper.updateAuth(id,ispublish,issignup,isreport);
            authMapper.updateUser(id,workstatus,filed);
        }

    }

    public long transLong(Object o){
        long l=Long.valueOf(String.valueOf(o)).longValue();
        return l;
    }

    public void synchronizeAuth(){
        //获取友空间的全部数据，并且放到新集合里面
        SyncEcUser syncEcUser=SyncEcUser.getInstance();
        ArrayList<JSONObject> auths = new ArrayList<>();
        auths=syncEcUser.doGetUser();

        //得到集合的数据，绑定到权限对象里面；然后将对象传递到业务层；
        int a=auths.size();
        long id=0;
        int aa=0;
        Auth auth = new Auth();
        for(aa=0;aa<auths.size();aa++){
            id =  Integer.parseInt(auths.get(aa).getString("id"));
            LOG.info("authservice获取到用户信息id："+id);
            LOG.info("authservice获取用户信息：插入前："+authMapper.findById(id));

            if(authMapper.findById(id)==null){
                auth.setName(auths.get(aa).getString("name"));
                auth.setId(id);
                authMapper.insertAuthOne(auth);
            }else {
                auth.setName(auths.get(aa).getString("name"));
                auth.setId(id);
                authMapper.updateAuthById(auth);
            }
            LOG.info("authservice获取到用户信息：插入后："+authMapper.findById(id));


         }
     }
}
